import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.Contract;

import java.io.IOException;
import java.util.Objects;

/**
 * @author NingWang
 * 2020-01-08 22:35
 */
public class HttpClient {

    private static final String[] USER_AGENT = new String[]{
      "User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
      "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
      "User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;",
      "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
      "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    };

    private static HttpClient client = new HttpClient();


    private  final OkHttpClient okHttpClient = new OkHttpClient();



    private HttpClient(){

    }


    public static HttpClient getInstance(){
        return HttpClient.client;
    }

     public String downloadM3U8(String url){
        Request request = new Request.Builder().url(url).header("User-Agent", getUserAgent())
                .addHeader("Host", "video1.yocoolnet.in")
                .addHeader("Referer", "https://video1.yocoolnet.in/api/player2.php?player=1&id=GEKt312SUVzVFA=&width=660&height=520&tid=140152")
                .build();


        try {
                Response response = okHttpClient.newCall(request).execute();
                if (!response.isSuccessful()) {
                    return null;
                }
                return Objects.requireNonNull(response.body()).string();
         }catch (IOException|NullPointerException e){
            return null;
        }
     }

     private String getUserAgent(){
         int index=(int)(Math.random()*USER_AGENT.length);

        return USER_AGENT[index];
     }



}
